<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SpringMVC架构深度解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.7;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #282c34;
            border-left: 4px solid #6e8efb;
        }
        .icon-highlight {
            color: #6e8efb;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .dropdown-content {
            display: none;
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        .dropdown:hover .dropdown-content {
            display: block;
            opacity: 1;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <header class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl px-6">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <span class="inline-block py-1 px-3 rounded-full bg-white bg-opacity-20 text-sm font-medium mb-4">技术架构</span>
                    <h1 class="text-4xl md:text-5xl font-bold leading-tight mb-6">SpringMVC架构深度解析</h1>
                    <p class="text-xl opacity-90 mb-8">深入理解SpringMVC的核心组件与运行机制，掌握请求处理的完整生命周期。</p>
                    <div class="flex space-x-4">
                        <a href="#architecture" class="bg-white text-purple-700 hover:bg-gray-100 px-6 py-3 rounded-lg font-medium transition duration-300">架构解析</a>
                        <a href="#demo" class="border-2 border-white text-white hover:bg-white hover:bg-opacity-20 px-6 py-3 rounded-lg font-medium transition duration-300">流程图解</a>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="bg-white bg-opacity-10 p-6 rounded-xl backdrop-blur-sm">
                        <div class="mermaid">
                            graph LR
                            A[客户端请求] --> B[DispatcherServlet]
                            B --> C[HandlerMapping]
                            C --> D[HandlerAdapter]
                            D --> E[Controller]
                            E --> F[ModelAndView]
                            F --> G[ViewResolver]
                            G --> H[视图渲染]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-6 py-16">
        <!-- Architecture Overview -->
        <section id="architecture" class="mb-20">
            <div class="flex items-center mb-10">
                <div class="w-12 h-1 bg-purple-600 mr-4"></div>
                <h2 class="text-3xl font-bold">架构概述</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-10 card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                            <i class="fas fa-project-diagram text-purple-600 text-lg"></i>
                        </div>
                        <h3 class="text-2xl font-semibold">SpringMVC架构概述</h3>
                    </div>
                    <p class="text-gray-700 mb-6">SpringMVC 是 Spring 框架的一个重要组成部分，是一个基于请求驱动的框架，用于处理 Web 请求。SpringMVC 基于 <strong class="text-purple-600">前端控制器模式</strong>，所有的请求都通过 <code class="bg-gray-100 px-2 py-1 rounded">DispatcherServlet</code> 进行分发。</p>
                    
                    <div class="grid md:grid-cols-2 gap-6 mt-8">
                        <div class="bg-gray-50 p-6 rounded-lg">
                            <div class="flex items-center mb-3">
                                <div class="w-8 h-8 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-exchange-alt text-blue-500"></i>
                                </div>
                                <h4 class="font-semibold text-lg">前端控制器模式</h4>
                            </div>
                            <ul class="space-y-3 text-gray-700">
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span><strong>前端控制器</strong>（DispatcherServlet）负责接收所有 HTTP 请求并将它们委派给具体的处理器</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span><strong>处理器映射器</strong>（HandlerMapping）根据请求的 URL 定位到相应的处理器（Controller）</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span><strong>处理器适配器</strong>（HandlerAdapter）用于将请求适配为 Controller 的方法调用</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span><strong>视图解析器</strong>（ViewResolver）将返回的逻辑视图名称解析为具体的视图</span>
                                </li>
                            </ul>
                        </div>
                        <div class="bg-gray-50 p-6 rounded-lg">
                            <div class="flex items-center mb-3">
                                <div class="w-8 h-8 rounded-full bg-green-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-sitemap text-green-500"></i>
                                </div>
                                <h4 class="font-semibold text-lg">核心优势</h4>
                            </div>
                            <ul class="space-y-3 text-gray-700">
                                <li class="flex items-start">
                                    <i class="fas fa-star text-yellow-500 mt-1 mr-2"></i>
                                    <span>清晰的职责分离，各组件耦合度低</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-star text-yellow-500 mt-1 mr-2"></i>
                                    <span>高度可配置，支持多种视图技术</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-star text-yellow-500 mt-1 mr-2"></i>
                                    <span>强大的数据绑定和表单处理能力</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-star text-yellow-500 mt-1 mr-2"></i>
                                    <span>灵活的拦截器机制</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- DispatcherServlet Section -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="w-12 h-1 bg-blue-600 mr-4"></div>
                <h2 class="text-3xl font-bold">DispatcherServlet：请求的中央控制器</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-10 card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                            <i class="fas fa-server text-blue-600 text-lg"></i>
                        </div>
                        <h3 class="text-2xl font-semibold">核心功能与生命周期</h3>
                    </div>
                    
                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">DispatcherServlet</code> 是 SpringMVC 的核心，负责请求的分发。它会协调所有组件，如 <code class="bg-gray-100 px-2 py-1 rounded">HandlerMapping</code>、<code class="bg-gray-100 px-2 py-1 rounded">HandlerAdapter</code> 和 <code class="bg-gray-100 px-2 py-1 rounded">ViewResolver</code>。</p>
                    
                    <div class="grid md:grid-cols-2 gap-8 mt-8">
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-cog text-blue-500 mr-2"></i>初始化和请求分发
                            </h4>
                            <ul class="space-y-3 text-gray-700 mb-6">
                                <li class="flex items-start">
                                    <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                                    <span>当用户发起请求时，<code class="bg-gray-100 px-1 py-0.5 rounded">DispatcherServlet</code> 会拦截请求</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                                    <span>使用 <code class="bg-gray-100 px-1 py-0.5 rounded">HandlerMapping</code> 来查找对应的处理器</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                                    <span>找到处理器后，使用 <code class="bg-gray-100 px-1 py-0.5 rounded">HandlerAdapter</code> 来执行该处理器</span>
                                </li>
                            </ul>
                            
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-recycle text-blue-500 mr-2"></i>生命周期
                            </h4>
                            <ul class="space-y-3 text-gray-700">
                                <li class="flex items-start">
                                    <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                                    <span>初始化时，读取 <code class="bg-gray-100 px-1 py-0.5 rounded">web.xml</code> 或 <code class="bg-gray-100 px-1 py-0.5 rounded">Spring配置文件</code> 中的配置</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                                    <span>加载应用上下文</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                                    <span>通过 AOP 拦截器链（<code class="bg-gray-100 px-1 py-0.5 rounded">HandlerInterceptor</code>）对请求进行处理</span>
                                </li>
                            </ul>
                        </div>
                        
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-code text-blue-500 mr-2"></i>关键源码实现
                            </h4>
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                                <pre><code class="language-java">public class DispatcherServlet extends HttpServlet {
    private WebApplicationContext webApplicationContext;

    @Override
    public void init() throws ServletException {
        // 初始化 Spring WebApplicationContext
        this.webApplicationContext = WebApplicationContextUtils
            .getWebApplicationContext(getServletContext());
    }

    @Override
    protected void service(HttpServletRequest request, 
                          HttpServletResponse response) 
                          throws ServletException, IOException {
        // 请求分发过程
        HandlerMapping handlerMapping = webApplicationContext
            .getBean(HandlerMapping.class);
        HandlerExecutionChain handlerExecutionChain = 
            handlerMapping.getHandler(request);
        
        if (handlerExecutionChain != null) {
            // 使用 HandlerAdapter 执行方法
            HandlerAdapter handlerAdapter = webApplicationContext
                .getBean(HandlerAdapter.class);
            handlerAdapter.handle(request, response, 
                handlerExecutionChain.getHandler());
        }
    }
}</code></pre>
                            </div>
                            <div class="mt-4 text-sm text-gray-600">
                                <i class="fas fa-info-circle mr-1"></i> <code class="bg-gray-100 px-1 py-0.5 rounded">DispatcherServlet</code> 负责处理 HTTP 请求并将它们转交给 <code class="bg-gray-100 px-1 py-0.5 rounded">HandlerMapping</code>。
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- HandlerMapping Section -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="w-12 h-1 bg-green-600 mr-4"></div>
                <h2 class="text-3xl font-bold">HandlerMapping：请求与处理器的映射</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-10 card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mr-4">
                            <i class="fas fa-route text-green-600 text-lg"></i>
                        </div>
                        <h3 class="text-2xl font-semibold">请求映射机制</h3>
                    </div>
                    
                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">HandlerMapping</code> 是 SpringMVC 中用于根据请求 URL 查找相应处理器的组件。SpringMVC 提供了多种 <code class="bg-gray-100 px-2 py-1 rounded">HandlerMapping</code> 实现，最常用的是 <code class="bg-gray-100 px-2 py-1 rounded">RequestMappingHandlerMapping</code>。</p>
                    
                    <div class="grid md:grid-cols-2 gap-8 mt-8">
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-map-signs text-green-500 mr-2"></i>请求映射的核心方法
                            </h4>
                            <p class="text-gray-700 mb-4"><code class="bg-gray-100 px-1 py-0.5 rounded">RequestMappingHandlerMapping</code> 是 SpringMVC 中最常用的 <code class="bg-gray-100 px-1 py-0.5 rounded">HandlerMapping</code> 实现，它会根据请求的 URL 匹配到具体的处理器方法。</p>
                            
                            <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 mb-6">
                                <div class="flex">
                                    <div class="flex-shrink-0">
                                        <i class="fas fa-lightbulb text-yellow-500"></i>
                                    </div>
                                    <div class="ml-3">
                                        <p class="text-sm text-yellow-700">
                                            Spring 5.3+ 默认使用 <code class="bg-yellow-100 px-1 py-0.5 rounded">RequestMappingHandlerMapping</code> 来处理 <code class="bg-yellow-100 px-1 py-0.5 rounded">@RequestMapping</code> 注解的控制器方法。
                                        </p>
                                    </div>
                                </div>
                            </div>
                            
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-cogs text-green-500 mr-2"></i>匹配策略
                            </h4>
                            <ul class="space-y-3 text-gray-700">
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                    <span>精确路径匹配（如 <code class="bg-gray-100 px-1 py-0.5 rounded">/users</code>）</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                    <span>路径模式匹配（如 <code class="bg-gray-100 px-1 py-0.5 rounded">/users/*</code>）</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                    <span>扩展名匹配（如 <code class="bg-gray-100 px-1 py-0.5 rounded">.html</code>）</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                    <span>HTTP 方法匹配（GET、POST等）</span>
                                </li>
                            </ul>
                        </div>
                        
                        <div>
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                                <pre><code class="language-java">public class RequestMappingHandlerMapping 
    implements HandlerMapping {
    @Override
    public HandlerExecutionChain getHandler(
        HttpServletRequest request) throws Exception {
        
        // 根据请求 URL 查找对应的处理器方法
        String uri = request.getRequestURI();
        for (Map.Entry&lt;String, Object&gt; entry : 
             handlerMap.entrySet()) {
            String pattern = entry.getKey();
            if (uri.matches(pattern)) {
                return new HandlerExecutionChain(
                    entry.getValue());
            }
        }
        return null;
    }
}</code></pre>
                            </div>
                            <div class="mt-4 p-4 bg-gray-50 rounded-lg">
                                <h5 class="font-medium text-gray-800 mb-2 flex items-center">
                                    <i class="fas fa-question-circle text-gray-500 mr-2"></i>常见问题
                                </h5>
                                <p class="text-sm text-gray-600 mb-2">如何自定义匹配规则？</p>
                                <p class="text-xs text-gray-500">可以继承 <code class="bg-gray-200 px-1 py-0.5 rounded">RequestMappingHandlerMapping</code> 并重写 <code class="bg-gray-200 px-1 py-0.5 rounded">getHandlerMethod</code> 方法来实现自定义的匹配逻辑。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- HandlerAdapter Section -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="w-12 h-1 bg-yellow-600 mr-4"></div>
                <h2 class="text-3xl font-bold">HandlerAdapter：请求与处理器方法的适配</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-10 card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-yellow-100 flex items-center justify-center mr-4">
                            <i class="fas fa-adjust text-yellow-600 text-lg"></i>
                        </div>
                        <h3 class="text-2xl font-semibold">适配器模式实现</h3>
                    </div>
                    
                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">HandlerAdapter</code> 是用于处理器方法适配的接口，它将请求和响应传递给相应的控制器方法。SpringMVC 通过不同的 <code class="bg-gray-100 px-2 py-1 rounded">HandlerAdapter</code> 实现来适配不同类型的处理器。</p>
                    
                    <div class="grid md:grid-cols-2 gap-8 mt-8">
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-puzzle-piece text-yellow-500 mr-2"></i>适配器模式
                            </h4>
                            <p class="text-gray-700 mb-4">适配器模式允许不兼容的接口能够一起工作。<code class="bg-gray-100 px-1 py-0.5 rounded">HandlerAdapter</code> 作为适配器，使得不同类型的处理器能够以统一的方式处理请求。</p>
                            
                            <div class="bg-blue-50 border-l-4 border-blue-400 p-4 mb-6">
                                <div class="flex">
                                    <div class="flex-shrink-0">
                                        <i class="fas fa-info-circle text-blue-500"></i>
                                    </div>
                                    <div class="ml-3">
                                        <p class="text-sm text-blue-700">
                                            SpringMVC 默认提供多种适配器实现，如 <code class="bg-blue-100 px-1 py-0.5 rounded">SimpleControllerHandlerAdapter</code>、<code class="bg-blue-100 px-1 py-0.5 rounded">HttpRequestHandlerAdapter</code> 和 <code class="bg-blue-100 px-1 py-0.5 rounded">RequestMappingHandlerAdapter</code>。
                                        </p>
                                    </div>
                                </div>
                            </div>
                            
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-tasks text-yellow-500 mr-2"></i>主要职责
                            </h4>
                            <ul class="space-y-3 text-gray-700">
                                <li class="flex items-start">
                                    <i class="fas fa-check text-yellow-500 mt-1 mr-2"></i>
                                    <span>检查处理器是否支持（<code class="bg-gray-100 px-1 py-0.5 rounded">supports</code> 方法）</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-yellow-500 mt-1 mr-2"></i>
                                    <span>执行处理器方法（<code class="bg-gray-100 px-1 py-0.5 rounded">handle</code> 方法）</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-yellow-500 mt-1 mr-2"></i>
                                    <span>处理返回值并生成 <code class="bg-gray-100 px-1 py-0.5 rounded">ModelAndView</code></span>
                                </li>
                            </ul>
                        </div>
                        
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-code text-yellow-500 mr-2"></i>关键源码实现
                            </h4>
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                                <pre><code class="language-java">public class RequestMappingHandlerAdapter 
    implements HandlerAdapter {
    
    @Override
    public boolean supports(Object handler) {
        return (handler instanceof HandlerMethod);
    }

    @Override
    public ModelAndView handle(HttpServletRequest request,
        HttpServletResponse response, Object handler) 
        throws Exception {
        
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Object controller = getController(handlerMethod);
        
        // 通过反射调用处理器方法
        Object result = handlerMethod.invoke(controller, 
            request.getParameterMap());
            
        return new ModelAndView("viewName", "result", result);
    }
}</code></pre>
                            </div>
                            <div class="mt-4 flex items-start">
                                <div class="flex-shrink-0">
                                    <i class="fas fa-exclamation-triangle text-orange-500 mt-1"></i>
                                </div>
                                <div class="ml-3">
                                    <p class="text-sm text-gray-600">
                                        实际实现比示例复杂得多，包括参数解析、数据绑定、验证、返回值处理等。
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Controller Section -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="w-12 h-1 bg-red-600 mr-4"></div>
                <h2 class="text-3xl font-bold">Controller：处理器和视图的结合</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-10 card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-red-100 flex items-center justify-center mr-4">
                            <i class="fas fa-cogs text-red-600 text-lg"></i>
                        </div>
                        <h3 class="text-2xl font-semibold">控制器实现方式</h3>
                    </div>
                    
                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">Controller</code> 是 SpringMVC 中用于处理请求的核心组件。SpringMVC 提供了多种方式来创建 Controller，例如通过实现 <code class="bg-gray-100 px-2 py-1 rounded">Controller</code> 接口或者使用 <code class="bg-gray-100 px-2 py-1 rounded">@Controller</code> 注解标注类。</p>
                    
                    <div class="grid md:grid-cols-2 gap-8 mt-8">
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-magic text-red-500 mr-2"></i>注解式控制器
                            </h4>
                            <p class="text-gray-700 mb-4">在现代 SpringMVC 应用中，使用 <code class="bg-gray-100 px-1 py-0.5 rounded">@Controller</code> 注解是最常见的控制器实现方式。</p>
                            
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto mb-4">
                                <pre><code class="language-java">@Controller
public class MyController {
    @RequestMapping("/hello")
    public String handleRequest(
        HttpServletRequest request) {
        return "helloView";
    }
}</code></pre>
                            </div>
                            <p class="text-sm text-gray-600">在 SpringMVC 中，<code class="bg-gray-100 px-1 py-0.5 rounded">@RequestMapping</code> 注解用于将 HTTP 请求映射到控制器方法。</p>
                            
                            <h4 class="font-semibold text-lg mt-6 mb-3 flex items-center">
                                <i class="fas fa-database text-red-500 mr-2"></i>数据传递与绑定
                            </h4>
                            <p class="text-gray-700">SpringMVC 支持模型（Model）的传递。返回的视图可以通过 <code class="bg-gray-100 px-1 py-0.5 rounded">ModelAndView</code> 或者 <code class="bg-gray-100 px-1 py-0.5 rounded">@ModelAttribute</code> 将数据传递到视图层。</p>
                        </div>
                        
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-box-open text-red-500 mr-2"></i>ModelAndView 实现
                            </h4>
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                                <pre><code class="language-java">public class ModelAndView {
    private String viewName;
    private Map&lt;String, Object&gt; model;

    public ModelAndView(String viewName, 
                       String modelName, 
                       Object modelObject) {
        this.viewName = viewName;
        this.model = new HashMap&lt;&gt;();
        this.model.put(modelName, modelObject);
    }
}</code></pre>
                            </div>
                            <div class="mt-4 bg-green-50 border-l-4 border-green-400 p-4">
                                <div class="flex">
                                    <div class="flex-shrink-0">
                                        <i class="fas fa-check-circle text-green-500"></i>
                                    </div>
                                    <div class="ml-3">
                                        <p class="text-sm text-green-700">
                                            <code class="bg-green-100 px-1 py-0.5 rounded">ModelAndView</code> 是控制器处理结果的容器，包含视图名称和模型数据。
                                        </p>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="mt-6">
                                <h5 class="font-medium text-gray-800 mb-2">常用注解</h5>
                                <div class="grid grid-cols-2 gap-2">
                                    <span class="bg-gray-100 px-3 py-1 rounded-full text-sm">@Controller</span>
                                    <span class="bg-gray-100 px-3 py-1 rounded-full text-sm">@RestController</span>
                                    <span class="bg-gray-100 px-3 py-1 rounded-full text-sm">@RequestMapping</span>
                                    <span class="bg-gray-100 px-3 py-1 rounded-full text-sm">@GetMapping</span>
                                    <span class="bg-gray-100 px-3 py-1 rounded-full text-sm">@PostMapping</span>
                                    <span class="bg-gray-100 px-3 py-1 rounded-full text-sm">@ModelAttribute</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- ViewResolver Section -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="w-12 h-1 bg-purple-600 mr-4"></div>
                <h2 class="text-3xl font-bold">ViewResolver：视图的解析</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-10 card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                            <i class="fas fa-eye text-purple-600 text-lg"></i>
                        </div>
                        <h3 class="text-2xl font-semibold">视图解析机制</h3>
                    </div>
                    
                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">ViewResolver</code> 是 SpringMVC 中用于解析视图名的接口。它的实现会根据返回的逻辑视图名解析出具体的视图对象，例如 JSP、Thymeleaf 等。</p>
                    
                    <div class="grid md:grid-cols-2 gap-8 mt-8">
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-file-alt text-purple-500 mr-2"></i>视图解析的实现
                            </h4>
                            <p class="text-gray-700 mb-4">SpringMVC 提供了多种 <code class="bg-gray-100 px-1 py-0.5 rounded">ViewResolver</code> 实现，最常用的是 <code class="bg-gray-100 px-1 py-0.5 rounded">InternalResourceViewResolver</code>，用于解析 JSP 视图。</p>
                            
                            <div class="bg-pink-50 border-l-4 border-pink-400 p-4 mb-6">
                                <div class="flex">
                                    <div class="flex-shrink-0">
                                        <i class="fas fa-list-ul text-pink-500"></i>
                                    </div>
                                    <div class="ml-3">
                                        <p class="text-sm text-pink-700">
                                            其他常用视图解析器：<code class="bg-pink-100 px-1 py-0.5 rounded">ThymeleafViewResolver</code>、<code class="bg-pink-100 px-1 py-0.5 rounded">FreeMarkerViewResolver</code>、<code class="bg-pink-100 px-1 py-0.5 rounded">ContentNegotiatingViewResolver</code>。
                                        </p>
                                    </div>
                                </div>
                            </div>
                            
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-cog text-purple-500 mr-2"></i>配置示例
                            </h4>
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                                <pre><code class="language-xml">&lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
    &lt;property name="prefix" value="/WEB-INF/views/" /&gt;
    &lt;property name="suffix" value=".jsp" /&gt;
&lt;/bean&gt;</code></pre>
                            </div>
                        </div>
                        
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-code text-purple-500 mr-2"></i>关键源码实现
                            </h4>
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                                <pre><code class="language-java">public class InternalResourceViewResolver 
    implements ViewResolver {
    
    private String prefix = "/WEB-INF/views/";
    private String suffix = ".jsp";

    @Override
    public View resolveViewName(String viewName, 
                               Locale locale) 
                               throws Exception {
        return new InternalResourceView(
            prefix + viewName + suffix);
    }
}</code></pre>
                            </div>
                            <div class="mt-4 p-4 bg-gray-50 rounded-lg">
                                <h5 class="font-medium text-gray-800 mb-2">工作原理</h5>
                                <p class="text-sm text-gray-600">
                                    <code class="bg-gray-100 px-1 py-0.5 rounded">InternalResourceViewResolver</code> 会将逻辑视图名（如 <code class="bg-gray-100 px-1 py-0.5 rounded">helloView</code>）解析成一个具体的视图对象（如 <code class="bg-gray-100 px-1 py-0.5 rounded">WEB-INF/views/helloView.jsp</code>）。
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Interceptor Section -->
        <section class="mb-20" id="demo">
            <div class="flex items-center mb-10">
                <div class="w-12 h-1 bg-indigo-600 mr-4"></div>
                <h2 class="text-3xl font-bold">SpringMVC 中的拦截器：HandlerInterceptor</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-10 card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                            <i class="fas fa-filter text-indigo-600 text-lg"></i>
                        </div>
                        <h3 class="text-2xl font-semibold">拦截器机制</h3>
                    </div>
                    
                    <p class="text-gray-700 mb-6">SpringMVC 提供了 <code class="bg-gray-100 px-2 py-1 rounded">HandlerInterceptor</code> 机制，在请求处理前后进行拦截操作。可以用于权限校验、日志记录等。</p>
                    
                    <div class="grid md:grid-cols-2 gap-8 mt-8">
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-layer-group text-indigo-500 mr-2"></i>拦截器链
                            </h4>
                            <p class="text-gray-700 mb-4">多个拦截器组成一个拦截器链，按照配置顺序依次执行。</p>
                            
                            <div class="bg-gray-100 p-4 rounded-lg mb-6">
                                <div class="mermaid">
                                    graph LR
                                    A[请求] --> B[拦截器1 preHandle]
                                    B --> C[拦截器2 preHandle]
                                    C --> D[处理器方法]
                                    D --> E[拦截器2 postHandle]
                                    E --> F[拦截器1 postHandle]
                                    F --> G[视图渲染]
                                    G --> H[拦截器2 afterCompletion]
                                    H --> I[拦截器1 afterCompletion]
                                </div>
                            </div>
                            
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-check-circle text-indigo-500 mr-2"></i>典型应用场景
                            </h4>
                            <ul class="space-y-3 text-gray-700">
                                <li class="flex items-start">
                                    <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                    <span>权限验证</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                    <span>日志记录</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                    <span>性能监控</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                    <span>全局异常处理</span>
                                </li>
                            </ul>
                        </div>
                        
                        <div>
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-code text-indigo-500 mr-2"></i>拦截器实现
                            </h4>
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                                <pre><code class="language-java">public class MyInterceptor 
    implements HandlerInterceptor {
    
    @Override
    public boolean preHandle(
        HttpServletRequest request,
        HttpServletResponse response, 
        Object handler) throws Exception {
        
        System.out.println("Pre-handle logic");
        return true; // 继续执行请求
    }

    @Override
    public void postHandle(
        HttpServletRequest request,
        HttpServletResponse response, 
        Object handler, 
        ModelAndView modelAndView) throws Exception {
        
        System.out.println("Post-handle logic");
    }

    @Override
    public void afterCompletion(
        HttpServletRequest request,
        HttpServletResponse response, 
        Object handler, 
        Exception ex) throws Exception {
        
        System.out.println("After completion logic");
    }
}</code></pre>
                            </div>
                            <div class="mt-4 grid grid-cols-3 gap-2">
                                <div class="bg-indigo-50 p-3 rounded-lg">
                                    <h6 class="text-xs font-semibold text-indigo-800 mb-1">preHandle</h6>
                                    <p class="text-xs text-indigo-600">请求处理前执行</p>
                                </div>
                                <div class="bg-indigo-50 p-3 rounded-lg">
                                    <h6 class="text-xs font-semibold text-indigo-800 mb-1">postHandle</h6>
                                    <p class="text-xs text-indigo-600">请求处理后执行</p>
                                </div>
                                <div class="bg-indigo-50 p-3 rounded-lg">
                                    <h6 class="text-xs font-semibold text-indigo-800 mb-1">afterCompletion</h6>
                                    <p class="text-xs text-indigo-600">请求完成后执行</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="w-12 h-1 bg-gray-800 mr-4"></div>
                <h2 class="text-3xl font-bold">SpringMVC 完整请求流程</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-10 card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-gray-100 flex items-center justify-center mr-4">
                            <i class="fas fa-sitemap text-gray-800 text-lg"></i>
                        </div>
                        <h3 class="text-2xl font-semibold">请求生命周期</h3>
                    </div>
                    
                    <div class="mermaid mb-10">
                        sequenceDiagram
                        participant Client
                        participant DispatcherServlet
                        participant HandlerMapping
                        participant HandlerAdapter
                        participant Controller
                        participant ViewResolver
                        participant View
                        
                        Client->>DispatcherServlet: HTTP 请求
                        DispatcherServlet->>HandlerMapping: 查找处理器
                        HandlerMapping-->>DispatcherServlet: 返回处理器链
                        DispatcherServlet->>HandlerAdapter: 执行处理器
                        HandlerAdapter->>Controller: 调用处理逻辑
                        Controller-->>HandlerAdapter: 返回ModelAndView
                        HandlerAdapter-->>DispatcherServlet: 返回ModelAndView
                        DispatcherServlet->>ViewResolver: 解析视图
                        ViewResolver-->>DispatcherServlet: 返回视图对象
                        DispatcherServlet->>View: 渲染视图
                        View-->>DispatcherServlet: 返回渲染结果
                        DispatcherServlet-->>Client: HTTP 响应
                    </div>
                    
                    <div class="grid md:grid-cols-3 gap-6">
                        <div class="bg-blue-50 p-6 rounded-lg">
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-sign-in-alt text-blue-500 mr-2"></i>请求进入阶段
                            </h4>
                            <ol class="list-decimal list-inside space-y-2 text-gray-700">
                                <li>客户端发送 HTTP 请求</li>
                                <li>DispatcherServlet 接收请求</li>
                                <li>HandlerMapping 查找匹配的处理器</li>
                            </ol>
                        </div>
                        <div class="bg-green-50 p-6 rounded-lg">
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-cogs text-green-500 mr-2"></i>处理阶段
                            </h4>
                            <ol class="list-decimal list-inside space-y-2 text-gray-700" start="4">
                                <li>HandlerAdapter 执行处理器方法</li>
                                <li>控制器处理业务逻辑</li>
                                <li>返回 ModelAndView</li>
                            </ol>
                        </div>
                        <div class="bg-purple-50 p-6 rounded-lg">
                            <h4 class="font-semibold text-lg mb-3 flex items-center">
                                <i class="fas fa-sign-out-alt text-purple-500 mr-2"></i>响应阶段
                            </h4>
                            <ol class="list-decimal list-inside space-y-2 text-gray-700" start="7">
                                <li>ViewResolver 解析视图</li>
                                <li>视图渲染</li>
                                <li>返回 HTTP 响应</li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-6">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-semibold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">深入解析技术原理，分享开发经验</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            sequence: {
                diagramMarginX: 50,
                diagramMarginY: 10,
                boxMargin: 10,
                useMaxWidth: true,
                rightAngles: false,
                showSequenceNumbers: false
            }
        });
    </script>
</body>
</html>